package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.demo.model.ExcelModel;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author huangzhuoguang
 * @Description
 * @date 2021/1/6
 */
@RestController
@RequestMapping("writeExcel")
public class WriteExcelController {
    /**
     * 普通的导出
     *
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "writeExcel")
    public void writeExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购订单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + UUID.randomUUID().toString() + ".xlsx");
        List<ExcelModel> list = new ArrayList<>();
        list.add(new ExcelModel("1", "hzg", "21"));
        list.add(new ExcelModel("2", "hzg1", "22"));
        list.add(new ExcelModel("3", "hzg2", "23"));

        EasyExcel.write(response.getOutputStream(), ExcelModel.class).sheet("采购订单").doWrite(list);
    }

    /**
     * 去除其中一个的导出
     *
     * @param response
     * @throws IOException
     */
    @GetMapping(value = "writeExcel2")
    public void writeExcel2(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购订单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + UUID.randomUUID().toString() + ".xlsx");
        List<ExcelModel> list = new ArrayList<>();
        list.add(new ExcelModel("1", "hzg", "21"));
        list.add(new ExcelModel("2", "hzg1", "22"));
        list.add(new ExcelModel("3", "hzg2", "23"));
        HashSet<String> objects = new HashSet<>();
        objects.add("age");
        EasyExcel.write(response.getOutputStream(), ExcelModel.class).excludeColumnFiledNames(objects).sheet("采购订单").doWrite(list);
    }

    /**
     * 导出含有多个sheet的excel表
     */
    @GetMapping(value = "writeExcel3")
    public void writeExcel3(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购订单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + UUID.randomUUID().toString() + ".xlsx");
        List<ExcelModel> list = new ArrayList<>();
        list.add(new ExcelModel("1", "hzg", "21"));
        list.add(new ExcelModel("2", "hzg1", "22"));
        list.add(new ExcelModel("3", "hzg2", "23"));
        ExcelWriter writer = EasyExcel.write(response.getOutputStream()).build();
        //导入第一个sheet
        WriteSheet sheet1 = EasyExcel.writerSheet(0, "采购单0").head(ExcelModel.class).build();
        writer.write(list, sheet1);
        //导入第一个sheet
        HashSet<String> objects = new HashSet<>();
        objects.add("ag1111e");
        WriteSheet sheet2 = EasyExcel.writerSheet(1, "采购单1").head(ExcelModel.class).excludeColumnFiledNames(objects).build();
        writer.write(list, sheet2);
        writer.finish();

    }

}
